//
//  CircleView.m
//  CircleView
//
//  Created by 钟进林 on 15/12/30.
//  Copyright © 2015年 紫霞大仙. All rights reserved.
//

#import "ZJLCircleView.h"

@interface ZJLCircleView ()
@property (nonatomic,strong)CAShapeLayer *shapeLayer;
@property (nonatomic,strong)CAShapeLayer *backgroundShapeLayer;
@end

@implementation ZJLCircleView
@synthesize backgroundShapeLayer = _backgroundShapeLayer;
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //背景图
        _backgroundShapeLayer        = [CAShapeLayer layer];
        _backgroundShapeLayer.frame  = self.bounds;
        
        // 创建贝塞尔曲线
        UIBezierPath *pathBackgroud = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
        _backgroundShapeLayer.path   = pathBackgroud.CGPath;
        _backgroundShapeLayer.fillColor = [UIColor clearColor].CGColor;
        _backgroundShapeLayer.lineWidth = 10.f;
        _backgroundShapeLayer.strokeColor = [UIColor grayColor].CGColor;//填充颜色
        _backgroundShapeLayer.strokeStart = 0.2f;
        _backgroundShapeLayer.strokeEnd = 1.0f;
        //添加到当前view
        [self.layer addSublayer:_backgroundShapeLayer];
        
        //进度条背景
        _shapeLayer        = [CAShapeLayer layer];
        _shapeLayer.frame  = self.bounds;
        
        // 创建贝塞尔曲线
        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
        _shapeLayer.path   = path.CGPath;
        _shapeLayer.fillColor = [UIColor clearColor].CGColor;
        _shapeLayer.lineWidth = 10.f;
        _shapeLayer.strokeColor = [UIColor grayColor].CGColor;//填充颜色
        _shapeLayer.strokeStart = 0.2f;
        _shapeLayer.strokeEnd = 0.f;
        //添加到当前view
        [self.layer addSublayer:_shapeLayer];
        //旋转
        self.transform = CGAffineTransformMakeRotation(M_PI * 0.3);
    }
    return self;
}
@synthesize startValue = _startValue;
- (void)setStartValue:(CGFloat)startValue {
    _startValue = startValue ;
    _shapeLayer.strokeStart = startValue;
}
- (CGFloat)startValue {
    return _startValue;
}
@synthesize lineWidth = _lineWidth;
- (void)setLineWidth:(CGFloat)lineWidth {
    _lineWidth = lineWidth;
    _shapeLayer.lineWidth  = lineWidth;
    _backgroundShapeLayer.lineWidth = lineWidth;
}
- (CGFloat)lineWidth {
    return _lineWidth;
}

@synthesize lineColor = _lineColor;
- (void)setLineColor:(UIColor *)lineColor {
    _lineColor = lineColor;
    _shapeLayer.strokeColor = lineColor.CGColor;
}
- (UIColor *)lineColor{
    return _lineColor;
}
@synthesize value = _value;
- (void)setValue:(CGFloat)value {
 
    //为什么是这样显示呢 ,因为有0.2的基础值
    _value = value*0.8 + 0.2;
    _shapeLayer.strokeEnd = value*0.8 + 0.2; ;
    
}
- (CGFloat)value{
    return _value;
}
@end
